package view;

import utils.Tools;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class AddClassView {
    private JFrame frame;
    private JTextField classNameField;
    private final int WIDTH = 400;
    private final int HEIGHT = 300;

    public AddClassView() {
        frame = new JFrame("新建班级");
        initView();
        frame.setLayout(null);
        frame.setResizable(false);
        Tools.setPos(frame, WIDTH, HEIGHT);
        //释放并关闭，子窗口
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    private void initView() {
        // 创建提示标签
        JLabel tipLabel = new JLabel("请输入新增的班级：");
        tipLabel.setBounds(50, 80, 150, 30);
        frame.add(tipLabel);

        // 创建文本输入框
        classNameField = new JTextField();
        classNameField.setBounds(200, 80, 150, 30);
        frame.add(classNameField);

        // 创建提交按钮
        JButton submitBtn = new JButton("提交");
        submitBtn.setBounds(150, 150, 100, 30);
        submitBtn.addActionListener(e -> saveClass());
        frame.add(submitBtn);

        // 创建返回按钮
        JButton backBtn = new JButton("返回主页");
        backBtn.setBounds(150, 200, 100, 30);
        backBtn.addActionListener(e -> {
            frame.dispose();
            new MainView();
        });
        frame.add(backBtn);
    }

    //新增班级的逻辑
    private void saveClass() {
        String className = classNameField.getText().trim();
        if (className.isEmpty()) {
            JOptionPane.showMessageDialog(frame, "班级名称不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        try {
            // 检查班级是否已存在
            if (isClassExists(className)) {
                JOptionPane.showMessageDialog(frame, "该班级已存在！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 将新班级写入文件
            FileWriter fw = new FileWriter("resources/class.txt", true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(className);
            bw.newLine();
            bw.close();
            fw.close();

            JOptionPane.showMessageDialog(frame, "班级添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
            classNameField.setText(""); // 清空输入框

        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, "保存失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    //检查班级是否存在
    private boolean isClassExists(String className) {
        try {
            File file = new File("resources/class.txt");
            if (!file.exists()) {
                return false;
            }

            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                //逐行比较班级名称是否相同
                if (line.trim().equals(className)) {
                    br.close();
                    return true;
                }
            }
            br.close();
            return false;
        //异常信息
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}